package com.wuwei.tms;

import lombok.SneakyThrows;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import java.net.URL;


@SpringBootApplication
@ComponentScan(basePackages = "com.wuwei")
@MapperScan({"com.wuwei.tms.setting.mapper","com.wuwei.tms.workbench.mapper"})
@EnableTransactionManagement
public class TmsApplication {

    public static void main(String[] args) {

// 因为springboot默认是不支持图形界面的，设置系统属性，启用图形界面

        System.setProperty("java.awt.headless", "false");

        TmsApplication eLearningApplication = new TmsApplication ();

        eLearningApplication.initSystemTray();

        SpringApplication.run(TmsApplication.class, args);
    }

  /**
     * 初始化系统托盘
     */
    @SneakyThrows
    private  void initSystemTray() {
        if (SystemTray.isSupported()) {
            URL resource = this.getClass().getClassLoader().getResource("tms.png");
            ImageIcon imageIcon = new ImageIcon(resource,"photo");
            Image image = imageIcon.getImage();
            TrayIcon trayIcon = new TrayIcon(image);
            trayIcon.setImageAutoSize(true);
            trayIcon.setToolTip("TMS工具管理系统");
            //设置托盘菜单
            PopupMenu pm = new PopupMenu() ;
            MenuItem mi = new MenuItem("Exit") ;
            MenuItem mi2 = new MenuItem("Enter");

            ActionListener listener = new ActionListener() {
                @SneakyThrows
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 处理菜单点击事件
                    if (e.getActionCommand().equals("Exit")) {
                        // 点击了Exit菜单

                        System.exit(0);
                    } else if (e.getActionCommand().equals("Enter")) {
                        // 点击了Enter菜单
                        // 执行相应的操作

                        URI uri = new URI("http://localhost");

                        Desktop desktop = Desktop.getDesktop();

                        desktop.browse(uri);

                    }
                }
            };

            pm.add(mi2);

            pm.add(mi);

            mi.addActionListener(listener);
            mi2.addActionListener(listener);
            trayIcon.setPopupMenu(pm);
            trayIcon.addMouseListener(new MouseAdapter(){
                @SneakyThrows
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        URI uri = new URI("http://localhost");

                        Desktop desktop = Desktop.getDesktop();

                        desktop.browse(uri);
                    }
                }
            });

            SystemTray.getSystemTray().add(trayIcon);
        } else {
            System.out.println("系统不支持托盘");
        }
    }

}
